package com.ruoyi.common.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * RSA加密解密
 *
 * @author ruoyi
 **/
public class RsaUtils {
    // Rsa 私钥
    public static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtaaziIV39MwGQ\n" +
            "Bp7YU/zhPKnzT+68hGjVw30BH2KtwqQiOmSwJQL+aeikTVaxKT8qq1/xoMA7En0G\n" +
            "zp2sKz70bF927CgiNsBH2aWXOg/rudyfV224tdTrD9Vr+m3xh5u3OFmp+nsNWzkg\n" +
            "bCRd/VxfF/D13Fr0Xx+2UIQdJDEEVyfzvTtcneIY1EFynyBL5mh+JqPxefVaziRT\n" +
            "IFvoODh/ZJoGt5qh/taX1BZeSBcbt6AePjG0LnO2IGcVrwYLnl5wEMuc7cTJO9j0\n" +
            "/O8B3lgCPsUM03vTDXN5rO6dfTah4xBr5Oa04ZqDvdK2iKmw1s7zNt54NuWBdPXE\n" +
            "1ysDHmZfAgMBAAECggEABPVqniS6HU1iFA48aI8uQfqMGYsFlppqrQIkBu4w0iso\n" +
            "qlnlGyxTw5afsmMLwZEeX0f7PFuOY6IJtTIfTBcIe9DFgg8Krr/GC8frdkGQxvrP\n" +
            "ljbXylpYtkgDoEzO7AMmrryLw9KHxj//iC/PcmiWRZnXqBAfAbHepEX1ISgxxp/z\n" +
            "UKNURlMNvCQT93p/3j9oacKIFMEKqShDTvus139uK9EmL3D8mm3Bp/kI4mOyKua3\n" +
            "byuGG6kpmEACclWhNdCKN8dz7jLYbCWcOdWD6koqdSZy10Dbf6BD0FPhVJ9oFRC2\n" +
            "g7l9vlihZJB9BkrZhb/+bDOhhb2kyrPUlVR6dxRzgQKBgQDfMeTGX8Bdi8MIEH3u\n" +
            "RbPt/vDR//sXFfEYt/p0gqCWZuAnT2eDo14pAuuQjc0idoQxpiazmAZR9/33td1L\n" +
            "KYKb5WBogb56+J7bI/w0u0MpG3TCyd6QIr1XXsDOuaEzhjKmwW3TwN1UI8YNjMGl\n" +
            "vb36Vy3PL3wYV+fFE28iI5ScfwKBgQDG5qQmE6I4+zKKloXTqfNGs0PGkOXzAYbr\n" +
            "eFDSePQGMb7qTWtnrBGAz9PqKXffzLIn/u2EO2cSheglSWfJKos55Fj/tFnTGuws\n" +
            "ELJZldOi1kXGeABK+Ur+yl3T3kti8PWXx2zDvmp98m0I0C5Qo+51gUDVLPnXOVAW\n" +
            "lE0DvhTGIQKBgQChac/ttynDfWQa3PJ182rg39MBzIKme3PVUJiM0juZCeVqCH7w\n" +
            "MKHUPwxlimekWNnZnjO3BmbruNGu55OW2/pdDFHgwpQjEtKv4SJwcE1t2TFj/eI7\n" +
            "kyQjn58I4i62uILBHnpy/bQTOZvuCLGzT7mo5ot2nvy8DjbLy/Y/VkQPuQKBgQCq\n" +
            "X1vSBdVaK8myLpc5pITvVZTDnNBrpC42utu4PcUBHVFkj6Zlql1f0iW6mYTGHieo\n" +
            "3NQFBYEXu74LWmJdTWTu1OtrNMgQbSpJ4I9070LWY+Zmx9mwXvPfZeaTGgHHIZGt\n" +
            "TjSbQxsjASpmdzSE/LbAzlODhsAHnF8+yA9kGkjfIQKBgEA2GzJIUnuzT8WTdSZL\n" +
            "rJ0/6SBkSd9rlQvBx2jWa4G92zD6++MeItbgEp8xlRKWzRfwGmEnt7R0SC3eGzWV\n" +
            "EToc3UVRi750Cd0qNo7Q7blfnzGuca16t5lTaeeCp3P8FVlDbhg1F5ijrDNItoSH\n" +
            "wWlvkBwJ0C1nQsBVrV08quVy";

    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text             待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text) throws Exception {
        return decryptByPrivateKey(privateKey, text);
    }


    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text             待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.decodeBase64(text));
        return new String(result);
    }

}